// @vue/component
import {
	List,
	Save,
	Update,
	Delete
} from '../../api/product.js'
import Dialog from "../../components/Dialog/Dialog.vue"
import Header from '../../components/Header/Header.vue'
import {
	mixin
} from "../../utils/mixins";
export default {
	name: 'Product',

	components: {
		Dialog,
		Header
	},

	mixins: [mixin],

	props: {},

	data() {
		return {
			dataForm: {
				key: ''
			},
			productList: [],
			pageIndex: 1,
			pageSize: 10,
			totalCount: 0,
			options: {
				list: ["分类ID", "状态ID","产品名称", "单价", "数量", "生产商", "型号", "类型", "生产日期", "入库日期", "描述信息"],
				data: {
					categoryId: "",
					statusId: null,
					name: "",
					price: "",
					amount: null,
					manufacturer: "",
					model: "",
					type: "",
					productDate: "",
					storageDate: "",
					description: '',
				},
				upload: false,
				type: "借出"
			},
		}
	},

	computed: {},

	watch: {},

	created() {
		this.getProductList()
	},

	methods: {
		showDialog(type, data, id) {
			console.log(data)
			if (type == "update") {
				data = this.$objFilter({}, ["amount", "categoryId", "manufacturer", "model", "description", "name",
						"price", "productDate","statusId","storageDate","type"
					],
					data, id)
				console.log(data)
				this.$refs.dialog.showDialog(type, data, id);
			} else {
				this.$refs.dialog.showDialog(type);
			}
		},
		getProductList: function() {
			List({
				'page': this.pageIndex,
				'limit': this.pageSize,
				'key': this.dataForm.key
			}).then(response => {
				this.productList = response.page.list
				this.totalCount = response.page.totalCount
				console.log("Productlist==========res")
				console.log(response)
			})
		},
		saveProductList: function() {
			let params = this.options.data
			Save(params).then(res => {
				this.notify("添加成功", "success");
				this.getProductList()
			})
		},
		updateProductList: function(id) {
			let params = JSON.parse(JSON.stringify(this.options.data));
			params["id"] = id
			Update(params).then(res => {
				this.notify("修改成功", "success");
				this.getProductList()
			})
			console.log(this.options)

		},
		getProductUserById: function() {

		},
		deleteById: function(id) {
			let ids = [];
			ids.push(id);
			console.log("===>" + ids)
			Delete(ids).then(response => {
				if (response.code === 200) {
					this.notify("删除成功", "success");
					this.getProductList();
				} else {
					this.notify("删除失败", "fail");
				}

			})
		},
		handleSizeChange(val) {
			this.pageSize = val
			this.getProductList();
		},
		handleCurrentChange(val) {
			this.pageIndex = val
			this.getProductList();
		},
		handleDelete(index, row) {
			this.deleteById(row.id);
		},
		search(e) {
			this.dataForm.key = e
			this.getProductList()
		}
	}
}
